﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataAccessLayer;
using BussinessLayer;

namespace QLBaoHanh.QLDanhMuc
{
    public partial class frmLoiDetail : Form
    {
        private bool _isEditForm;
        private Loi _loi;
        private int _loiId;
        private frmDsLoi _callerForm;

        public int LoiId
        {
            set
            {
                _loiId = value;
                //reload Loi
                _loi = LoiBO.getInstance.GetLoiById(_loiId);
                RefreshForm();
            }
        }

        public bool IsEditForm
        {
            set
            {
                _isEditForm = value;
                if (!_isEditForm)
                    FillFormLoi();
            }
        }

        public frmDsLoi CallerForm
        {
            get { return _callerForm; }
            set { _callerForm = value; }
        }

        public frmLoiDetail()
        {
            InitializeComponent();
            _isEditForm = false;
            _loiId = 0;
            _loi = null;
        }

        private void FillFormLoi()
        {
            if (_isEditForm)
            {
                txtMaKH.Text = _loi.Ma;
                txtName.Text = _loi.Ten;
                txtMoTa.Text = _loi.MoTa;
            } 
            else
            {
                this.Text = "Thêm lỗi";
                txtTitle.Text = "Thêm mới lỗi";
                txtMaKH.Text = LoiBO.getInstance.GenerateMaLoi();
                txtName.Text = "";
                txtMoTa.Text = "";
            }
        }

        private bool IsDataHasChanged()
        {
            bool hasChange = false;
            //only check in case edit
            if(_isEditForm && _loi != null)
            {
                if (  txtName.Text != _loi.Ten
                   || txtMoTa.Text != _loi.MoTa )
                {
                    hasChange = true;
                }
            }
            else
            {
                if (txtName.Text != "" || txtMoTa.Text != "")
                {
                    hasChange = true;
                }
            }
            return hasChange;
        }

        private bool SaveLoi()
        {
            if (ValidateFormData())
            {
                if (IsDataHasChanged())
                {
                    if (_isEditForm)
                    {
                        //update  loi
                        _loi.Ten = txtName.Text.Trim();
                        _loi.MoTa = txtMoTa.Text.Trim();
                        LoiBO.getInstance.UpdateLoi(_loi);
                    } 
                    else
                    {
                        //insert loi
                        _loi = new Loi();
                        _loi.Ma = txtMaKH.Text.Trim();
                        _loi.Ten = txtName.Text.Trim();
                        _loi.MoTa = txtMoTa.Text.Trim();
                        LoiBO.getInstance.InsertLoi(_loi);
                        _callerForm.RefreshForm();
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        private bool ValidateFormData()
        {
            //hien thi message du lieu ko valid
            return true;
        }

        private void RefreshForm()
        {
            FillFormLoi();
        }

        private void frmLoiDetail_Load(object sender, EventArgs e)
        {
            //Neu la edit thi load form edit khong thi load form them moi
            if (_isEditForm)
            {
                if(_loi == null)
                {
                    return;
                }
            }
 
            FillFormLoi();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (SaveLoi())
            {
                this.Close();
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            RefreshForm();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if(IsDataHasChanged())
            {
                if (MessageBox.Show("Dữ liệu có sự thay đổi, Bạn có muốn lưu không?"
                , "Đóng", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    //save data
                    if (SaveLoi())
                    {
                        this.Close();
                    }
                }
                else
                {
                    //close form
                    this.Close();    
                }
            }
            else
            {
                //close form
                this.Close();
            }
        }
    }
}
